package com.yuanlrc.base.service.admin;

import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.dao.admin.BorrowDao;
import com.yuanlrc.base.dao.admin.ViolationDao;
import com.yuanlrc.base.entity.admin.Borrow;
import com.yuanlrc.base.entity.admin.Violation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class ViolationService {

    @Autowired
    private ViolationDao violationDao;

    /**
     * 逾期记录分页展示
     * @param violation
     * @param pageBean
     * @return
     */
    public PageBean<Violation> findByName(Violation violation, PageBean<Violation> pageBean) {
        ExampleMatcher withMatcher = ExampleMatcher.matching().withMatcher("bookName", ExampleMatcher.GenericPropertyMatchers.contains());
        withMatcher = withMatcher.withIgnorePaths("status");
        Example<Violation> example = Example.of(violation, withMatcher);
        Pageable pageable = PageRequest.of(pageBean.getCurrentPage() - 1, pageBean.getPageSize());
        Page<Violation> findAll = violationDao.findAll(example, pageable);
        for (Violation violation1 : findAll) {
             violation1.setBookName(violation1.getBook().getBookName());
        }
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }

    /**
     * 逾期记录删除
     * @param id
     */
    public void delete(Long id) {
        violationDao.deleteById(id);
    }


}
